package com.ztsjglxt.controller.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ztsjglxt.core.IMenu;
import com.ztsjglxt.core.Ibasecommon;
import com.ztsjglxt.entity.Menu;
import com.ztsjglxt.entity.utils.JsonWrite;
import com.ztsjglxt.utils.TreeUnit;

import net.sf.json.JSONArray;

/**
 * 菜单类控制器
 * @author bxf
 *
 */
@Controller
@RequestMapping("sys/sysmenu")
public class MenuController extends BaseController{
	private Menu infonow = new Menu();
	boolean isSuccess = true;
	@Autowired
	private Ibasecommon basebll;
	@Autowired
	private IMenu menuservice;

	

	
	/**
	 * 编辑页面
	 * 
	 * @author bxf
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(HttpServletRequest request,
			HttpServletResponse response, String pid) {

		request.setAttribute( "buttom", outputbtn(pid, basebll));
		return "menu/edit";
	}

	/**
	 * 获取列表
	 * 
	 * @author bxf
	 */
	@RequestMapping(value = "/getlist", method = RequestMethod.POST)
	public void getlist(HttpServletResponse response) {
		List<Map> lst = this.basebll
				.getlistfromsql("select id, menu_Name, pid,enu_Order, href, menu_icon,Is_delete, comment from menu");
		jsonWrite.setOthervalue(JSONArray.fromObject(lst).toString());
		jsonWrite.setSuccess(true);
		this.writeJson(response, jsonWrite);
	}

	/**
	 * 获取树列表
	 * 
	 * @author bxf
	 */
	@RequestMapping(value = "/getTree", method = RequestMethod.POST)
	public void getTree(HttpServletResponse response,
			HttpServletRequest request, Integer kind) {

		List<Map> lst;
			lst = this.basebll
					.getlistfromsql("select id, menu_Name, pid,menu_Order, href, menu_icon,Is_delete, comment from menu where pid=0 and Is_delete=1 and kind=1 order by menu_Order");
		List<TreeUnit> list = new ArrayList<TreeUnit>();
		TreeUnit model = new TreeUnit();
		model.setTitle("首页");
		model.setPid("0");
		model.setIcon("larry-houtaishouye");
		model.setHref("");
		list.add(model);
		for (Map map : lst) {
			model = new TreeUnit();
			model.setTitle(map.get("menu_Name").toString());
			model.setPid(map.get("pid").toString());
			model.setIcon(map.get("menu_icon").toString());
			if (map.get("href").toString().indexOf("?") > 0) {
				model.setHref(map.get("url").toString() + "&pid="
						+ map.get("id").toString());
			} else {
				model.setHref(map.get("href").toString() + "?pid="
						+ map.get("id").toString());
			}
			GetChildren(kind, lst, map, model);
			list.add(model);
		}
		jsonWrite.setObj(list);
		jsonWrite.setSuccess(true);
		this.writeJson(response, jsonWrite);
	}

	/**
	 * 获取树列表（编辑时候调用）
	 * 
	 * @author bxf
	 */
	@RequestMapping(value = "/gettreeedit", method = RequestMethod.POST)
	private void gettreeedit(HttpServletResponse response,
			HttpServletRequest request) {
		List<Map> lstone = this.basebll
				.getlistfromsql("select pid,menu_Name name,(select menu_Name from menu t2 where t2.id=t1.pid) as menunamelast,(select max(menu_Order) from menu t3 where t3.pid=t1.id) maxsortCode  ,t1.id,t1.Is_delete, t1.comment,t1.kind,t1.menu_Order,t1.href event,t1.href event,t1.menu_icon icon from menu t1 where  pid=0  order by menu_Order  ");
		List<Map> lsttwo = new ArrayList<Map>();
		List<Map> lstlast = new ArrayList<Map>();

		for (int i = 0; i < lstone.size(); i++) {
			lsttwo = this.basebll
					.getlistfromsql("select  pid,menu_Name name,(select menu_Name from menu t2 where t2.id=t1.pid) as menunamelast,(select max(menu_Order) from menu t3 where t3.pID=t1.id) maxsortCode ,t1.id,t1.Is_delete, t1.comment,t1.menu_Order,t1.kind,t1.href event,t1.href event,t1.menu_icon icon  from menu t1 where pid= "
							+ lstone.get(i).get("id").toString()
							+ "  order by menu_Order ");
			for (int j = 0; j < lsttwo.size(); j++) {
				lstlast = this.basebll
						.getlistfromsql("select  pid,menu_Name name,(select menu_Name from menu t2 where t2.id=t1.pid) as menunamelast,(select max(menu_Order) from menu t3 where t3.pid=t1.id) maxsortCode ,t1.id,t1.Is_delete,t1.menu_Order, t1.comment,t1.kind,t1.href event,t1.menu_icon icon  from menu t1 where pid= "
								+ lsttwo.get(j).get("id").toString()
								+ "  order by menu_Order ");
				lsttwo.get(j).put("children", lstlast);
			}
			lstone.get(i).put("children", lsttwo);
		}
		jsonWrite.setOthervalue(JSONArray.fromObject(lstone).toString());
		jsonWrite.setSuccess(true);
		this.writeJson(response, jsonWrite);
	}


	/**
	 * 获取树权限
	 * 
	 * @author bxf
	 * @date 2018-07-06
	 */
	private void GetChildren(Integer kind, List<Map> lst, Map map,
			TreeUnit model) {
		List<TreeUnit> list = new ArrayList<TreeUnit>();
		List<Map> lst2 = this.basebll
					.getlistfromsql("select id, menu_Name, pid,menu_Order, href, menu_icon,Is_delete, comment from menu where pid="
							+ map.get("id")
							+ " and Is_delete=1 and kind=1 order by menu_Order");

		for (Map map2 : lst2) {
			TreeUnit model2 = new TreeUnit();
			model2.setTitle(map2.get("menu_Name").toString());
			model2.setPid(map2.get("pid").toString());
			model2.setIcon(map2.get("menu_icon").toString());

			if (map2.get("href").toString().indexOf("?") > 0) {
				model2.setHref(map2.get("href").toString() + "&pid="
						+ map2.get("id").toString());
			} else {
				model2.setHref(map2.get("href").toString() + "?pid="
						+ map2.get("id").toString());
			}
			list.add(model2);
		}
		if (list.size() > 0) {
			model.setChildren(list);
			model.setSpread(true);
		}
	}

	/**
	 * 新增菜单
	 * 
	 * @author bxf
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(HttpServletResponse response, HttpServletRequest request,
			Menu record) {
		int count = menuservice.insert(record);
		JsonWrite jsonWrite = new JsonWrite();
		if(count>0){
			jsonWrite.setSuccess(true);
		}else{
			jsonWrite.setSuccess(false);
		}
		this.writeJson(response, jsonWrite);
	}

	/**
	 * 修改
	 * @param request
	 * @param response
	 * @param entity
	 * @throws Exception
	 */
	@RequestMapping("/updatemeu")
	public void updatestate(HttpServletRequest request,HttpServletResponse response,Menu entity) throws Exception{
		int count = menuservice.updatemeu(entity);
		JsonWrite jsonwrite = new JsonWrite();
		if(count>0){
			jsonwrite.setSuccess(true);
		}else{
			jsonwrite.setSuccess(false);
		}
		this.writeJson(response, jsonwrite);
	}
	
	/**
	 * 删除菜单
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deletemenu")
	public void deletemenu(HttpServletRequest request,
			HttpServletResponse response,Integer id) {
		int count = menuservice.deleteByPrimaryKey(id);
		JsonWrite jsonwrite = new JsonWrite();
		if(count>0){
			jsonwrite.setSuccess(true);
		}else{
			jsonwrite.setSuccess(false);
		}
		this.writeJson(response, jsonwrite);
	}
}
